package com.sj.uabrowser.mvp.main.view.adp;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.sj.uabrowser.R;
import com.sj.uabrowser.db.Search;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class SearchContentAdp extends BaseAdapter {

    List<Search> searches;

    public SearchContentAdp(List<Search> searches) {
        this.searches = searches;
    }

    @Override
    public int getCount() {
        return searches.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.search_content_item, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }
        holder = (ViewHolder) convertView.getTag();
        Search search = searches.get(position);
        String title = search.getTitle();
        String content = search.getContent();
        if(TextUtils.isEmpty(title)) {
            holder.tvTitle.setText(content);
            holder.tvContent.setVisibility(View.GONE);

        }else {
            holder.tvContent.setVisibility(View.VISIBLE);
            holder.tvTitle.setText(title);
            holder.tvContent.setText(content);
        }
        return convertView;
    }

    class ViewHolder {
        @BindView(R.id.iv_type)
        ImageView ivType;
        @BindView(R.id.tv_title)
        TextView tvTitle;
        @BindView(R.id.tv_content)
        TextView tvContent;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
